package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.*;

@Service
@Transactional

public class PermissionServiceImpl implements IPermissionService, ApplicationContextAware {

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private ApplicationContext ac;

    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    @Override
//    public ResultPage<Permission> query(QueryObject qo) {
//        int totalCount = permissionMapper.selForCount(qo);
//        if (totalCount == 0) {
//            return new ResultPage<>(qo.getCurrentPage(), qo.getPageSize());
//        }
//        List<Permission> data = permissionMapper.selForList(qo);
//        return new ResultPage<>(qo.getCurrentPage(), qo.getPageSize(), totalCount, data);
//
//    }
    public PageInfo<Permission> query(QueryObject qo) {
        //使用分页插件
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Permission> permissions = permissionMapper.selForList(qo);
        return new PageInfo<>(permissions);
    }

    @Override
    public void reload() {
        List<Permission> list = permissionMapper.selectAll();
        Set<String> set = new HashSet<>();
        for (Permission p : list) {
            set.add(p.getExpression());
        }
        //拿到容器中所有对象.  已有的Controller都在容器中
        //拿到容器的两种方式
        //①注入ApplicationContext对象
        //②实现ApplicationContextAware接口,  重写setApplicationContext()方法

        //1.拿到所有Controller
//        RequestMappingHandlerMapping rmhm = ac.getBean(RequestMappingHandlerMapping.class);
//        Map<RequestMappingInfo, HandlerMethod> handlerMethods = rmhm.getHandlerMethods();
//        Collection<HandlerMethod> methods = handlerMethods.values();
//        List<Permission> expressionList = new ArrayList<>();
//        //2.遍历控制器方法
//        for (HandlerMethod method : methods) {
//            //3.获取自定义注解
//            RequirePermission rp = method.getMethodAnnotation(RequirePermission.class);
//            //4.判空上面方法是存在自定义注解.
//            if (rp != null) {
//                String name = rp.name();
//                String expression = rp.expression();
//                if (StringUtils.isNotNull(name) && StringUtils.isNotNull(expression)) {
//                    //防止重复添加数据
//                    if (!set.contains(expression)) {
//                        Permission permission = new Permission();
//                        permission.setName(name);
//                        permission.setExpression(expression);
//                        expressionList.add(permission);
//                    }
//
//                }
//            }
//        }
//        if (expressionList != null && expressionList.size() > 0) {
//            permissionMapper.insertPermissionBatch(expressionList);
//        }

    }

    @Override
    public List<String> selEmployeePermissionById(Long id) {
        return permissionMapper.selEmployeePermissionById(id);
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ac = applicationContext;
    }
}
